package org.example.view;

import org.example.controller.IndexController;
import org.example.data.Account;
import org.example.data.User;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class ManageIncome extends JFrame {
    private JTextField idField, dateField, contentField, amountField;
    private JComboBox<String> typeComboBox;
    private JButton addButton;
    private IndexController indexController;

    public ManageIncome(User user) {
        setTitle("管理收支");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        // 创建界面元素
        JPanel panel = new JPanel(new GridLayout(5, 2, 10, 10));
        idField = new JTextField();
        dateField = new JTextField();
        typeComboBox = new JComboBox<>(new String[]{"收入", "支出"});
        contentField = new JTextField();
        amountField = new JTextField();
        addButton = new JButton("添加");
        panel.add(new JLabel("日期:"));
        panel.add(dateField);
        panel.add(new JLabel("类型:"));
        panel.add(typeComboBox);
        panel.add(new JLabel("内容:"));
        panel.add(contentField);
        panel.add(new JLabel("金额:"));
        panel.add(amountField);
        panel.add(addButton);

        indexController = new IndexController(); // 初始化 IndexController

        // 添加按钮的点击事件监听
        addButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String date = dateField.getText();
                String type = (String) typeComboBox.getSelectedItem();
                String content = contentField.getText();
                Double amount = Double.parseDouble(amountField.getText());
                // 创建 Account 对象
                Account account = new Account( date, type, content, amount);
                // 调用 IndexController 的 insert 方法插入数据
                indexController.insert(account, user.getId());
                JOptionPane.showMessageDialog(ManageIncome.this, "信息添加成功", "提示", JOptionPane.INFORMATION_MESSAGE);
                dispose(); // 关闭当前登录界面
            }
        });

        add(panel);
        setSize(400, 300);
        setLocationRelativeTo(null);
        setVisible(true);
    }
    public Boolean status(){
        return true;
    }
}
